package com.team2.shoppingService;

import java.io.FileNotFoundException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.webharvest.definition.ScraperConfiguration;
import org.webharvest.runtime.Scraper;
import org.webharvest.runtime.variables.Variable;
import org.xml.sax.InputSource;

public class WebHarvest {
	
	public String GetProductCatalog(String searchString, String configFile){
		StringBuilder returnString = new StringBuilder();
		try{
			ScraperConfiguration config =
					new ScraperConfiguration(configFile);
			Scraper scraper = new Scraper(config, "./");
			scraper.addVariableToContext("searchString",searchString);
			scraper.setDebug(true);
			scraper.execute();

			Variable varScrappedContent =
					(Variable)scraper.getContext().getVar("scrappedContent");
			
			returnString.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
			returnString.append(varScrappedContent.toString());
  
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}
		return returnString.toString();
	}
}
